//
//  HLJIllustrationCell.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJIllustrationCell.h"



@interface HLJIllustrationCell ()

@property(nonatomic,retain)NSMutableArray *allImgArray;

@end

@implementation HLJIllustrationCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}


#pragma mark - 子视图
-(void)createSubView{
    
    
    
    //类型
    self.type = [[UILabel alloc]init];
    [self.contentView addSubview:self.type];
    [self.type mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(180, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.left.and.top.mas_equalTo(20);
        
        
    }];
    
    self.type.font = [UIFont systemFontOfSize:WIDTH/25];
    self.type.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    
    
    
    //头图
    self.headerImgView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * SCALE_WIDTH, 60, 340 * SCALE_WIDTH, 140 )];
    //    self.headerImgView.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.headerImgView];
    
    
    //取中间部分放入框中
    self.headerImgView.contentMode = UIViewContentModeScaleAspectFill;
    //剪裁边缘
    self.headerImgView.clipsToBounds = YES;
    self.headerImgView.userInteractionEnabled = YES;
    self.headerImgView.tag = 1001;
    
    
    //两个小图
    
    self.smallImgView = [[UIImageView alloc]initWithFrame:CGRectMake(20 * SCALE_WIDTH , self.headerImgView.origin.y + self.headerImgView.height + self.type.origin.y/3*2 + 10, self.headerImgView.size.width / 2- 5 , 140)];//(WIDTH - self.type.origin.x/3*8)/2 去掉两边加中间除以2 小图的宽
    //    self.smallImgView.backgroundColor = [UIColor greenColor];
    self.smallImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.smallImgView.clipsToBounds = YES;
    [self.contentView addSubview:self.smallImgView];
    
    self.smallImgView.userInteractionEnabled = YES;
    self.smallImgView.tag = 1002;
    
    
    self.brotherImgView = [[UIImageView alloc]initWithFrame:CGRectMake(self.headerImgView.origin.x + self.smallImgView.width + self.type.origin.x/2 + 10, self.headerImgView.origin.y + self.headerImgView.height + self.type.origin.y/3*2 + 10, (WIDTH - self.type.origin.x/3*8)/2 - 20, 140)];
    //    self.brotherImgView.backgroundColor = [UIColor greenColor];
    
    self.brotherImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.brotherImgView.clipsToBounds = YES;
    [self.contentView addSubview:self.brotherImgView];
 
    self.brotherImgView.userInteractionEnabled = YES;
    self.brotherImgView.tag = 1003;
    
    
    
    
    //imgBackGround
    self.imgBackGround = [[UIImageView alloc]initWithFrame:CGRectMake(self.brotherImgView.width - self.brotherImgView.width/4.5,self.brotherImgView.height - self.brotherImgView.width/4.5, self.brotherImgView.width/4.5, self.brotherImgView.width/4.5)];
    self.imgBackGround.backgroundColor = [UIColor grayColor];
    self.imgBackGround.alpha = 0.7;
    [self.brotherImgView addSubview:self.imgBackGround];

    
    
    
    //图数量
    self.imgCount = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.imgBackGround.height/3*2, self.imgBackGround.height/3*2)];
    self.imgCount.font = [UIFont systemFontOfSize:self.imgBackGround.height/2];
    self.imgCount.center = CGPointMake(self.imgBackGround.height/2 + self.imgBackGround.height/8     , self.imgBackGround.height/2);
    self.imgCount.textColor = [UIColor colorWithRed:0.95 green:0.94 blue:0.93 alpha:1];
    [self.imgBackGround addSubview:self.imgCount];
 
    
    
    
    
    //标题
    self.titleLb =[[UILabel alloc]init];
    
     [self.contentView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
       // make.size.mas_equalTo(CGSizeMake(120, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.top.mas_equalTo(370);
        
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(- 20);
    }];

    
    self.titleLb.font = [UIFont systemFontOfSize:18];
   

    
    
    
    //文字详情
    self.content = [[UILabel alloc]init];
    
    [self.contentView addSubview:self.content];
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height. mas_equalTo(70);
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo( - 70);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        
        
    }];

    
    
    self.content.font = [UIFont systemFontOfSize:17];
    self.content.textColor =  [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    self.content.numberOfLines = 3;
    
    
    
    
    //心形图
    self.heartButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: self.heartButton];
    
    [self.heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(30, 30));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-20);
        
        make.right.mas_equalTo(- 75);
        
        
        
    }];
    
    [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
    [self.heartButton addTarget:self action:@selector(heartButton:) forControlEvents:UIControlEventTouchUpInside];

    
    //赞数
    self.zanLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.zanLabel];
    
    [self.zanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size. mas_equalTo(CGSizeMake(35, 20));
        // 添加左、上边距约束（左、上约束都是20）
        make.bottom.mas_equalTo(-25);
        
        make.right.mas_equalTo(- 25);
        
        
        
    }];
    
    
    self.zanLabel.font = [UIFont systemFontOfSize:12];
    self.zanLabel.textColor = [UIColor colorWithRed:0.47 green:0.47 blue:0.47 alpha:1];
    
    

    
#pragma mark  轻拍手势
    //轻拍
    UITapGestureRecognizer *headerTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.headerImgView addGestureRecognizer:headerTap];
    
    //轻拍
    UITapGestureRecognizer *smallTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.smallImgView addGestureRecognizer:smallTap];
    
    //轻拍
    UITapGestureRecognizer *brotherTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self.brotherImgView addGestureRecognizer:brotherTap];
    
    
    
    
    
}




#pragma mark - 轻拍手势
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    
    [self.illCellDeleage presentToViewController:tap.view.tag ImgArray:self.allImgArray];
    
    
}





#pragma mark - set
-(void)setIllModel:(HLJHomeModel *)illModel{
    _illModel = illModel;
    self.type.text = [NSString stringWithFormat:@"%@ · %@",self.illModel.name,self.illModel.enname];
    
    [self.headerImgView sd_setImageWithURL:self.illModel.coverimg placeholderImage:PLACEHOLDER_IMAGE];
    self.titleLb.text = self.illModel.title;
    self.content.text = self.illModel.content;
    self.imgCount.text = [NSString stringWithFormat:@"%@",self.illModel.imgtotal];
    self.zanLabel.text = [NSString stringWithFormat:@"%@",self.illModel.like];
    
    self.allImgArray = [NSMutableArray array];
    //把图片地址取出来放数组里
    for (NSMutableDictionary *dic in self.illModel.imglist) {
        [self.allImgArray addObject:[dic objectForKey: @"imgurl"]];
    }
    
    
    NSString *str1 = [NSString stringWithFormat:@"%@", [[self.illModel.imglist objectAtIndex:1]objectForKey:@"imgurl"]];
    NSURL *url1 = [NSURL URLWithString:str1];
    [self.smallImgView sd_setImageWithURL:url1 placeholderImage:PLACEHOLDER_IMAGE];
    
    NSString *str = [NSString stringWithFormat:@"%@", [[self.illModel.imglist objectAtIndex:2]objectForKey:@"imgurl"]];
    NSURL *url = [NSURL URLWithString:str];
    [self.brotherImgView sd_setImageWithURL:url placeholderImage:PLACEHOLDER_IMAGE];
    
    //判断收藏状态
    NSMutableArray *array = [[ZZCCollectionData collectDataBase]selectCollectList];
    NSMutableArray *array2 = [NSMutableArray array];
    for (HLJHomeModel *model in array) {
        [array2 addObject:model._id];
    }
    if ([array2 containsObject: self.illModel._id]) {
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
    }
    
}

- (void)heartButton:(UIButton *)btn
{
    NSMutableArray *array = [[ZZCCollectionData collectDataBase] selectCollectList];
    NSMutableArray *amptyArray = [NSMutableArray array];
    
    for (HLJHomeModel *model in array) {
        [amptyArray addObject:model._id];
    }
    if ([amptyArray containsObject:self.illModel._id]) {
        [[ZZCCollectionData collectDataBase] deleteMyCollectData:self.illModel.title];
        
        CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
        keyAnimation.duration = 0.2;
        [btn.layer addAnimation:keyAnimation forKey:@"key"];
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan00-2.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] - 1];
    }else{
        HLJHomeModel *model = [[HLJHomeModel alloc]init];
        model._id = self.illModel._id;
        model.title = self.illModel.title;
        model.coverimg = self.illModel.coverimg;
        model.name = self.illModel.name;
        
        [[ZZCCollectionData collectDataBase]inserCollectList:model];
        
        //关键帧动画
        CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        //设置关键帧的值,values是数组类型
        //@(0.1)相当于NSNumber
        keyAnimtion.values = @[@(0.1),@(1.0),@(1.5)];//缩放的倍数
        keyAnimtion.duration = 0.2;
        [btn.layer addAnimation:keyAnimtion forKey:@"key"];
        
        
        [self.heartButton  setImage:[UIImage imageNamed:@"iconfont-xinxihuan002.png"] forState:UIControlStateNormal];
        self.zanLabel.text = [NSString stringWithFormat:@"%ld",[(self.zanLabel.text)integerValue] + 1];
    }
    
    
    
}


@end
